package com.instasale.models;

import org.hibernate.annotations.Cascade;
import org.springframework.context.annotation.Lazy;

import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "SELLERS")
@Lazy(false)
public class Seller implements Serializable {
    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;

    @NotNull(message = "Please enter title")
    @Size(min = 1, max = 50)
    @Column(name = "TITLE")
    private String title;

    @OneToMany(mappedBy = "seller", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    private Set<Car> cars = new HashSet<Car>();


    public Set<Car> getCars() {
        return cars;
    }

    public void setCars(Set<Car> cars) {
        this.cars = cars;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public int hashCode() {
        if(id == null)
            return super.hashCode();
        return 37 * id;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || id == null) {
            return false;
        }
        if (!(obj instanceof Seller)) {
            return false;
        }
        return id.equals(((Seller) obj).getId());
    }
}
